/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.ui;

import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import de.tor.tribes.reportserver.PersistenceImpl;
import de.tor.tribes.reportserver.util.UIUtil;
import de.tor.tribes.types.ParserConfiguration;
import de.tor.tribes.types.ParserPattern;
import de.tor.tribes.types.ParserPatternRelation;
import java.util.HashMap;
import java.util.Set;

/**
 *
 * @author Torridity
 */
public class ParserConfigurationScreen extends CustomComponent {

    private GridLayout mainLayout;
    private TextField serverField;
    private PersistenceImpl persistence;
    private HashMap<ParserPattern.PARSER_PATTERN_ID, TextField> fieldMap;
    private ParserConfiguration currentConfig;

    public ParserConfigurationScreen(PersistenceImpl pPersistence) {
        persistence = pPersistence;
        fieldMap = new HashMap<ParserPattern.PARSER_PATTERN_ID, TextField>();
        buildMainLayout();
        setCompositionRoot(mainLayout);
    }

    private void buildMainLayout() {
        UIUtil.GridLayoutBuilder builder = new UIUtil.GridLayoutBuilder(2, 14);
        serverField = new TextField("Server");
        serverField.setWidth("100%");
        Button load = new Button("Load", this, "loadConfiguration");
        load.setWidth("100%");
        builder = builder.addComponent(serverField, 0, 0, 1, 1).addComponent(load, 1, 0, 1, 1);

        int col = 0;
        int row = 1;
        for (ParserPattern.PARSER_PATTERN_ID pattern : ParserPattern.PARSER_PATTERN_ID.values()) {
            TextField f = new TextField(pattern.toString(), pattern.getDefault());
            f.setWidth("100%");
            fieldMap.put(pattern, f);
            builder.addComponent(f, col, row, 1, 1);
            col++;
            if (col == 2) {
                col = 0;
                row++;
            }
        }
        Button test = new Button("Text");
        test.setWidth("100%");
        Button save = new Button("Save", this, "saveConfiguration");
        save.setWidth("100%");
        mainLayout = builder.addComponent(test, 0, 13, 1, 1).addComponent(save, 1, 13, 1, 1).getLayout();
        //   mainLayout = new UIUtil.GridLayoutBuilder(2, 5).addComponent(patterns, 0, 0, 2, 4).addComponent(test, 0, 4, 1, 1).addComponent(save, 1, 4, 1, 1).getLayout();
        mainLayout.setSizeFull();
        mainLayout.setSpacing(true);
    }

    public void loadConfiguration() {
        String server = (String) serverField.getValue();

        ParserConfiguration config = persistence.getParserConfiguration(server);
        if (config.getId() == null) {
            //new config
            persistence.persist(config);
            currentConfig = config;
        } else {
            currentConfig = config;
        }
        Set<ParserPatternRelation> relations = config.getPatternRelations();
        for (ParserPatternRelation relation : relations) {
            TextField f = fieldMap.get(relation.getPattern().getPatternName());
            f.setValue(relation.getPatternValue());
        }
    }

    public void saveConfiguration() {
        for (ParserPattern.PARSER_PATTERN_ID pattern : ParserPattern.PARSER_PATTERN_ID.values()) {
            currentConfig.setPatternValue(pattern, (String) fieldMap.get(pattern).getValue(), persistence);
        }
        persistence.merge(currentConfig);
    }
}
